package com.controller.home;

import com.controller.BaseController;
import com.dao.*;
import com.entity.*;
import com.service.imple.home.ShopOrderGoodsServiceImple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 购物车
 */
@Controller
@RequestMapping("home/ShopOrderGoods")
public class ShopOrderGoodsController extends BaseController<ShopOrderGoodsServiceImple> {

    @Autowired
    private VipUserMapper vipUserMapper;
    @Autowired
    private UserCartLogMapper userCartLogMapper;
    @Autowired
    private ShopOrderMapper shopOrderMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;

    @RequestMapping("addShopOrderGoods")
    @ResponseBody
    public Map<String, Object> addShopOrderGoods(@SessionAttribute("userId")Integer userId, String goodsImg, String name,
                                                 BigDecimal goodsPrice, Integer totalNum, Integer goodsId, Integer catalogId, BigDecimal totalPrice,
                                                 String accept_name, String province, String city, String area, String address, String email,
                                                 String post_code, String message, BigDecimal totalAmount,String telphone,String param,Integer goodsNumber){

        Map<String,Object> map = new HashMap<>();
        VipUser vipUser = new VipUser();
        vipUser.setId(userId);
        List<VipUser> list= vipUserMapper.selectAll(vipUser);//查询余额

        BigDecimal RechargeMoney = list.get(0).getRechargeMoney();
        if ((RechargeMoney.compareTo(goodsPrice))== 1||(RechargeMoney.compareTo(goodsPrice))== 0){//判断余额是否足够
            BigDecimal RechargeMoney1 = RechargeMoney.subtract(goodsPrice);
            list.get(0).setRechargeMoney(RechargeMoney1);
            int count1 = vipUserMapper.updateByPrimaryKeyWithBLOBs(list.get(0));//减去余额
                if (count1>=1){
                    ShopOrder shopOrder = new ShopOrder();
                    shopOrder.setAddress(address);//详细地址
                    shopOrder.setAddTime(new Date());//时间
                    shopOrder.setArea(area);//区
                    shopOrder.setCity(city);//市
                    shopOrder.setEmail(email);//邮箱
                    shopOrder.setCode(post_code);//邮政编码
                    shopOrder.setPhone(telphone);//手机号码
                    shopOrder.setUserName(accept_name);//姓名
                    shopOrder.setProvince(province);//省
                    shopOrder.setTotalMoney(totalAmount);//总金额
                    shopOrder.setRemark(message);//备注
                    shopOrder.setShopState(1);
                    shopOrder.setUserId(userId);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String dateString = sdf.format(new Date());
                    shopOrder.setOrderSn(dateString);
                    int count2 = shopOrderMapper.insert(shopOrder);//添加订单表

                    if (count2>=1){
                        int count3 =0;
                        ShopOrder shopOrder1 = new ShopOrder();
                        shopOrder1.setOrderSn(dateString);
                        List<ShopOrder> list1= shopOrderMapper.Select(shopOrder1);
                        Integer orderId = list1.get(0).getId();

                        Map<String,Object> map1 = new HashMap<>();
                        map1.put("userId",userId);
                        List<UserCartLog> userCartLogs = userCartLogMapper.selectAll(map1);
                        ShopOrderGoods shopOrderGoods = new ShopOrderGoods();
                        Goods goods = new Goods();

                        for (UserCartLog log: userCartLogs) {
                            shopOrderGoods.setGoodsId(log.getGoodsId());//商品编号
                            shopOrderGoods.setUserId(userId);//会员编号
                            shopOrderGoods.setGoodsName(log.getGoodsName());//商品名称
                            shopOrderGoods.setGoodsSn(log.getGoodsSn());//商品货号
                            shopOrderGoods.setGoodsImg("http://localhost:8080/public/images"+log.getGoodsImg());//商品图片
                            shopOrderGoods.setCatalogId(log.getCatalogId());
                            shopOrderGoods.setPrice(BigDecimal.valueOf(log.getSellPrice()));//商品价格
                            shopOrderGoods.setTotalPrice(log.getSellPrice()*log.getGoNum());//总金额
                            shopOrderGoods.setGoNum(log.getGoNum());//商品数量
                            shopOrderGoods.setOrderId(orderId);//订单编号
                            shopOrderGoods.setAddTime(new Date());//添加时间
                            count3 = BaseService.insert(shopOrderGoods);//添加订单商品表

                            goods.setId(log.getGoodsId());
                            List<Goods> goodsList = goodsMapper.selectAll(goods);
                            goodsList.get(0).setGoodsNumber(goodsList.get(0).getGoodsNumber()-goodsNumber);
                            goodsMapper.updateByPrimaryKey(goodsList.get(0));//减去库存

                        }
                        if (param.equals("1")){
                            if (count3>=1){
                                UserCartLog userCartLog = new UserCartLog();
                                userCartLog.setUserId(userId);
                                int count4 = userCartLogMapper.delete(userCartLog);//删除购物车订单
                                map.put("msg",3);
                                return map;
                            }
                        }if (param.equals("2")){
                            map.put("msg",3);
                            return map;
                        } else {
                            map.put("msg","系统订单表错误购买失败");
                            return map;
                        }
                    }else {
                        map.put("msg","添加订单表错误购买失败");
                        return map;
                    }
                }else {
                    map.put("msg","系统减款错误购买失败");
                    return map;
                }
            }else {
                map.put("msg","余额不足请充值");
                return map;
            }
    }

    /**
     * 查看订单
     */
    @RequestMapping("orderDefault")
    public String orderDefault(Integer orderId, ModelMap modelMap){
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setId(orderId);
        List<ShopOrder> list = shopOrderMapper.Select(shopOrder);

        ShopOrderGoods shopOrderGoods = new ShopOrderGoods();
        shopOrderGoods.setOrderId(orderId);
        List<ShopOrderGoods> list1 = BaseService.SelectAll(shopOrderGoods);

        float TotalPrice = 0;
        for (ShopOrderGoods m : list1) {
            TotalPrice += m.getTotalPrice() * m.getGoNum();
            System.out.println(TotalPrice);
        }
        list1.get(0).getTotalPrice();
        modelMap.put("list",list);
        modelMap.put("list1",list1);
        modelMap.put("TotalPrice",TotalPrice);
        return "center/order/orderDefault";
    }

}
